Skip to content

Add native Permuto and multilevel LoD encodings - #538

Merged
njroussel merged 4 commits into
NVlabs:masterfrom
shsolanki-1:codex/permuto-lod-native
Sep 29, 2026
Merged

njroussel merged 4 commits into
NVlabs:masterfrom
shsolanki-1:codex/permuto-lod-native

Conversation

@shsolanki-1

@shsolanki-1 shsolanki-1 commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Add native Permuto training, parameter-gradient, input-gradient, and supported double-backward paths.
  • Generalize Permuto construction across input dimensions 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 16, 24 and features per level 1, 2, 4, 8.
  • Generalize MultiLevelEncodingLoD to wrap multilevel encodings with hard or soft level selection.
  • Chain NetworkWithInputEncoding double backward through its encoding and network. The network output is recomputed only on that path, so first-order input-gradient passes carry no extra state.
  • Implement double backward for the Identity encoding. The PyTorch tcnn.Network wraps its network in Identity, so double backward through it needs this path.
  • Preserve base layouts, matrix strides, parameter ownership, CUDA streams, gradient modes, empty batches, and serialized configuration.
  • Document the public configuration, execution, and derivative contracts.

Motivation

The existing Permuto factory exposes one fixed specialization, and the existing level-of-detail wrapper is tied to Permuto and hard level selection. This change exposes the existing native implementation over a defined configuration matrix and provides one native multilevel LoD wrapper for Permuto and Grid encodings.

Compatibility

The existing five-dimensional configuration with two features per level retains its parameter order, lattice tables, outputs, gradients, and serialized hyperparameters. n_features and n_grid_features are accepted as total-feature aliases and serialize to canonical n_levels and n_features_per_level fields.

The LoD ratio is scheduler state. Its input gradient remains zero. Nested LoD wrappers are rejected because their mutable level state does not compose consistently.

The PyTorch bindings compute the second-order input gradient whenever the input requires a gradient, including when all parameters are frozen. Previously that case skipped the native call and returned zero, which is wrong for encodings with a nonzero input Hessian such as Grid and Permuto. Modules without an input-Hessian implementation now raise instead of returning zero. Setting backward_backward_input_no_input_grad = True opts out and returns no input gradient. A frozen tcnn.Network with one hidden ReLU layer returns the exact zero input Hessian. Other FullyFusedMLP shapes raise, because the non-JIT network double backward supports only that shape.

Grid backward and double-backward kernels now disable a level with the same level >= max_level + 1e-3f predicate as the forward kernel, so a level whose output is zeroed no longer receives parameter gradients.

Known Issue

HashGrid double backward fails with a CUDA illegal memory access in kernel_grid_backward_input_backward_dLdoutput. The unmodified base commit fails the same way with a standalone tcnn.Encoding on CUDA 12.8, PyTorch 2.8, and an RTX PRO 6000 Blackwell. This PR does not change that kernel. Because NetworkWithInputEncoding now supports double backward, a HashGrid-based network reaches this kernel instead of raising an unsupported-operation error.

Validation

  • Earlier revision: configured and built Release tests with CUDA 12.8, GCC 12.3, and TCNN_CUDA_ARCHITECTURES=89. Passed test_permuto with 29 test cases and 425,577 assertions on an RTX 4090. Passed the focused non-JIT FullyFusedMLP double-backward test with 141,383 assertions.
  • Current revision, built with CUDA 13.0 and TCNN_CUDA_ARCHITECTURES=120 on an RTX PRO 6000 Blackwell: test_encodings (including the Identity double-backward test), test_grid, test_jit_losses, and test_permuto pass. In test_networks, the non-JIT FullyFusedMLP double-backward test and the NetworkWithInputEncoding empty-batch activation test pass. The existing JIT tolerance test Various invariance checks for neural networks fails with the same 200 assertions on the unmodified base commit. This PR does not modify the failing networks.
  • Current revision, PyTorch bindings built with CUDA 12.8 and PyTorch 2.8 for sm_120: bindings/torch/tests/test_permuto.py passes (9 tests), including frozen-parameter tcnn.Network double backward.
  • Earlier revision: an eikonal-style double backward through NetworkWithInputEncoding (hard LoD over Permuto, FullyFusedMLP with one 64-neuron hidden layer, JIT off) matched the preceding revision within run-to-run nondeterminism.
  • Verified the PR range with git diff --check.

Build Impact

The complete native matrix creates 104 Permuto specializations: 13 input dimensions, four feature widths, and two scalar types. In the measured Release build, clean build time increased by 1.2155x, encoding.cu.o increased by 2.6306x, and libtiny-cuda-nn.a increased by 1.6015x. The retained five-dimensional, two-feature specialization kept the same generated device-code size.

Generated inference is intentionally excluded from this PR and is provided by stacked follow-up #539. Generated backward and generated double backward remain unsupported.

Comment thread include/tiny-cuda-nn/encodings/grid.h Resolved
@shsolanki-1
shsolanki-1 force-pushed the codex/permuto-lod-native branch from 2031b7c to ef4e2df Compare September 29, 2026 08:22
@shsolanki-1
shsolanki-1 marked this pull request as ready for review September 29, 2026 08:31
Register the native Permuto encoding and a hard MultiLevelEncodingLoD
wrapper for training, with parameter-gradient, input-gradient, and double
backward paths. Add non-JIT FullyFusedMLP double backward for one hidden
ReLU layer without an output activation, and let the torch bindings skip
the second-order input gradient when callers do not need it.

NetworkWithInputEncoding chains double backward through its encoding and
network. Network::backward takes the network output, which double
backward does not receive, so it is recomputed from the saved encoding
output on that path. Storing a copy in forward instead would cost every
pass that prepares first-order input gradients.

The torch bindings compute the second-order input gradient whenever the
input requires a gradient, including when the parameters are frozen.
tcnn.Network wraps its network in an Identity encoding, so Identity now
implements double backward: it passes the scaled second-order gradient
upstream, and its input Hessian is zero because it is linear. Without it,
frozen-parameter tcnn.Network double backward would fail in the encoding.
Dispatch the existing native implementation over the public dimension and
feature-width matrix. Canonicalize aliases and validate configuration
before template construction while preserving the existing 5D/two-feature
artifact contract.

Cap AoS transpose geometry so wide outputs stay within CUDA block-size
limits.
Wrap any multilevel encoding so Grid and Permuto share one hard or soft
LoD path. Preserve base layouts, strides, parameters, streams, and double
backward by staging incompatible matrices through the preferred layout and
retaining unweighted soft outputs.

Expose level counts through the minimal base interface and align Grid
derivative cutoffs with the forward predicate at the epsilon boundary.
@shsolanki-1
shsolanki-1 force-pushed the codex/permuto-lod-native branch from ef4e2df to 5a69b4a Compare September 29, 2026 09:31
@njroussel
njroussel merged commit 3daa6e5 into NVlabs:master Sep 29, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants